<?php
/**
 * CreateTime: 2023/6/27 11:15
 */

namespace App\Tools\QxwgSMS;

use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Consts\TableConsts\TableConsts;
use App\Func\AppFunc;
use App\Func\ResponseFunc;
use App\Func\ScopeFunc\AuthScopeStaffFunc;
use App\Http\Requests\ListBasicRequest;
use App\Models\MeLawCase\MeLawCase;
use App\Models\SMS\SMSUpstreamModel;
use App\Scopes\MediationStaffScope;

class QxwgController
{
    private $client;

    public function __construct()
    {
        $this->client = QxwgSmsClient::getInstance();
    }

    private function replay(){

    }

    public function p2pSendSms($data){
        $this->client->changeRequestType('POST');
        $sendData = [];
        $phoneStr = '';
        foreach($data as $phone => $content){
            // 号码加密
//            $aesPhone = $this->client->smsEncryption($phone);
//            dump($aesPhone);
            // 拼接号码与内容
            $phoneStr .=   $phone.$content;
//            $sendData['mobileContentKvp'][$aesPhone] = $content;
            $sendData['mobileContentKvp'][$phone] = $content;
        }
//        dump(AppFunc::getQXWGConfig()::QXWG_SECRET. AppFunc::getQXWGConfig()::QXWG_XNJRM.$phoneStr);
        // 密码加密处理签名
//         $sendData['password'] = md5(AppFunc::getQXWGConfig()::QXWG_SECRET. AppFunc::getQXWGConfig()::QXWG_XNJRM.$phoneStr);
//         $sendData['mobileEencryptionMode'] = 'aes';
        // 明文密码传输
        $sendData['password'] = AppFunc::getQXWGConfig()::QXWG_SECRET;

        $res = $this->client->execute('p2p',$sendData);

        $fmtRes = json_decode($res,true);
        if($fmtRes['status']!=0){
            return false;
        }
        return $fmtRes['list'];
    }


    public function upstream(ListBasicRequest $request){
        $pFilters = $request->get('filters');
        $pageSize = $request->get('page_size');
//        $filters = $pFilters ? json_decode($pFilters, true) : [];
//        $this->client->changeRequestType('POST');
//        $sendData = [];
//        $sendData['password'] = AppFunc::getQXWGConfig()::QXWG_SECRET;
//        $res = $this->client->execute('mo',$sendData);
//        $fmtRes = json_decode($res,true);

        $smsUp = new SMSUpstreamModel();
       //  if(!$fmtRes['status']){
        // 暂时屏蔽运营商报错 用于返回列表
        //            return ResponseFunc::returnJson(
        //                'error',
        //                null,
        //                $fmtRes['message'],
        //                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
        //            );
      //  }else{
            // 当运营商没有返回报错的时候再填充数据进入数据库
//            foreach($fmtRes['list'] as $value){
//                $smsUp->add([
//                    'phone' => $value['mobile'],
//                    'content' => $value['content'],
//                    'receiveTime' => $value['time'],
//                ]);
//            }
//        }

        $query = $smsUp->select([
            'law_case_sms_upstream.law_case_sms_upstream_id',
            'law_case_sms_upstream.law_case_sms_upstream_id as key',
            'law_case_sms_upstream.phone',
            'law_case_sms_upstream.content',
            'law_case_sms_upstream.receiveTime',
            'phone.law_case_phone_id',
        ])
        ->leftjoin(TableConsts::ME_LAW_CASE_PHONE." as phone",'law_case_sms_upstream.phone','phone.phone_number')
        ->leftJoin(TableConsts::ME_LAW_CASE." as law_case",'law_case.main_respondent_phone_id','phone.law_case_phone_id');
        if(isset($filters['phone'])){
            $query = $query->where('phone','LIKE',"%".$filters['phone']."%");
        }
        $data = $query->withoutGlobalScope(MediationStaffScope::class)
                ->whereIn('law_case.owner_id',AuthScopeStaffFunc::getDepartmentStaffByCurrentStaffId())
                ->distinct()
                ->Paginate($pageSize)->toArray() ?? [];
        return ResponseFunc::returnJson(
            'ok',
            $data,
            "上行回复获取成功",
            ResponseStatusConsts::OK_200_RESPONSE_STATUS,
        );
    }
}